package cn.iocoder.yudao.module.common.service.gradesubjectrelation;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.common.controller.admin.gradesubjectrelation.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.gradesubjectrelation.GradeSubjectRelationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.common.dal.mysql.gradesubjectrelation.GradeSubjectRelationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.common.enums.ErrorCodeConstants.*;

/**
 * 年级学科 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GradeSubjectRelationServiceImpl implements GradeSubjectRelationService {

    @Resource
    private GradeSubjectRelationMapper gradeSubjectRelationMapper;

    @Override
    public Long createGradeSubjectRelation(GradeSubjectRelationSaveReqVO createReqVO) {
        // 插入
        GradeSubjectRelationDO gradeSubjectRelation = BeanUtils.toBean(createReqVO, GradeSubjectRelationDO.class);
        gradeSubjectRelationMapper.insert(gradeSubjectRelation);
        // 返回
        return gradeSubjectRelation.getId();
    }

    @Override
    public void updateGradeSubjectRelation(GradeSubjectRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateGradeSubjectRelationExists(updateReqVO.getId());
        // 更新
        GradeSubjectRelationDO updateObj = BeanUtils.toBean(updateReqVO, GradeSubjectRelationDO.class);
        gradeSubjectRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteGradeSubjectRelation(Long id) {
        // 校验存在
        validateGradeSubjectRelationExists(id);
        // 删除
        gradeSubjectRelationMapper.deleteById(id);
    }

    private void validateGradeSubjectRelationExists(Long id) {
        if (gradeSubjectRelationMapper.selectById(id) == null) {
            throw exception(GRADE_SUBJECT_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public GradeSubjectRelationDO getGradeSubjectRelation(Long id) {
        return gradeSubjectRelationMapper.selectById(id);
    }

    @Override
    public PageResult<GradeSubjectRelationDO> getGradeSubjectRelationPage(GradeSubjectRelationPageReqVO pageReqVO) {
        return gradeSubjectRelationMapper.selectPage(pageReqVO);
    }

}